﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DrugStorehouseMIS.DAL;
using DrugStorehouseMIS.Comm;
using DrugStorehouseMIS.DAL.Interface;

namespace DrugStorehouseMIS.BLL
{
    public class StockInHeader
    {
        #region 封装属性

        private string mStockInNum;

        public string StockInNum
        {
            get { return mStockInNum; }
            set { mStockInNum = value; }
        }

        private StockInType mStockInTypeName;

        public StockInType StockInTypeName
        {
            get { return mStockInTypeName; }
            set { mStockInTypeName = value; }
        }

        private Supplier mSupplierName;

        public Supplier SupplierName
        {
            get { return mSupplierName; }
            set { mSupplierName = value; }
        }

        private DateTime mStockInDatetime;

        public DateTime StockInDatetime
        {
            get { return mStockInDatetime; }
            set { mStockInDatetime = value; }
        }

        private string mCreatedBy;

        public string CreatedBy
        {
            get { return mCreatedBy; }
            set { mCreatedBy = value; }
        }

        private string mApprovalBy;

        public string ApprovalBy
        {
            get { return mApprovalBy; }
            set { mApprovalBy = value; }
        }

        private string mRemark;

        public string Remark
        {
            get { return mRemark; }
            set { mRemark = value; }
        }

        private string mStatus;

        public string Status
        {
            get { return mStatus; }
            set { mStatus = value; }
        }

        #endregion

        private IStockInHeaderDAL dal;

        public StockInHeader()
        {
            dal = Adapter.GetStockInHeader();
        }

        public void FetchObject(IDataReader dr)
        {
            if (dr != null)
            {
                mStockInNum = ConvertDataType.CheckFiledString(dr["stock_in_num"]);
                mStockInTypeName.StockInTypeName = ConvertDataType.CheckFiledString(dr["stock_in_type_name"]);
                mSupplierName.SupplierName = ConvertDataType.CheckFiledString(dr["supplier_name"]);
                mStockInDatetime = ConvertDataType.CheckFiledDateTime(dr["stock_in_datetime"]);
                mCreatedBy = ConvertDataType.CheckFiledString(dr["created_by"]);
                mApprovalBy = ConvertDataType.CheckFiledString(dr["approval_by"]);
                mRemark = ConvertDataType.CheckFiledString(dr["remark"]);
                mStatus = ConvertDataType.CheckFiledString(dr["status"]);
            }
        }

        /// <summary>
        /// 根据状态显示入库单header
        /// </summary>
        /// <param name="Status"></param>
        /// <returns></returns>
        public DataTable DisplayByStatus(string Status)
        {
            return dal.SelectByStatus(Status);
        }

        /// <summary>
        /// 实现多条件过滤
        /// </summary>
        /// <param name="StockInNum"></param>
        /// <param name="StockInTypeName"></param>
        /// <param name="SupplierName"></param>
        /// <param name="Status"></param>
        /// <returns></returns>
        public DataTable Fliter(string StockInNum, string StockInTypeName, string SupplierName, string Status)
        {
            return dal.Select(StockInNum, StockInTypeName, SupplierName, Status);
        }

        /// <summary>
        /// 根据单号获取入库单header
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public StockInHeader GetStockInHeader(string num)
        {
            StockInHeader obj = new StockInHeader();
            obj.StockInTypeName = new StockInType();
            obj.SupplierName = new Supplier();
            IDataReader dr = dal.Select(num);
            while (dr != null && dr.Read())
            {
                obj.FetchObject(dr);
            }
            return obj;
        }

        /// <summary>
        /// 改变入库单状态
        /// </summary>
        /// <returns></returns>
        public bool ChangeStatus(string StockInNum)
        {
            return dal.Update(StockInNum) > 0 ? true : false;
        }

        /// <summary>
        ///  创建入库单header
        /// </summary>
        /// <param name="stockInHeader"></param>
        /// <returns></returns>
        public bool CreateStockInHeader(StockInHeader stockInHeader)
        {
            return dal.Insert(stockInHeader.StockInNum, stockInHeader.StockInTypeName.StockInTypeName, stockInHeader.SupplierName.SupplierName, stockInHeader.StockInDatetime, stockInHeader.CreatedBy, stockInHeader.ApprovalBy, stockInHeader.Remark, stockInHeader.Status) > 0 ? true : false;
        }

        /// <summary>
        ///  删除入库单header同时会删除其detail（在存储过程里执行）
        /// </summary>
        /// <param name="stockInNum"></param>
        /// <returns></returns>
        public bool RemoveStockInHeader(string stockInNum)
        {
            return dal.Delete(stockInNum) > 0 ? true : false;
        }

        /// <summary>
        /// 更新入库单header
        /// </summary>
        /// <param name="stockInHeader"></param>
        /// <returns></returns>
        public bool SetStockInHeader(StockInHeader stockInHeader)
        {
            return dal.Update(stockInHeader.StockInNum, stockInHeader.StockInTypeName.StockInTypeName, stockInHeader.SupplierName.SupplierName, stockInHeader.StockInDatetime, stockInHeader.CreatedBy, stockInHeader.ApprovalBy, stockInHeader.Remark, stockInHeader.Status) > 0 ? true : false;
        }

    }
}
